<template lang="pug">
div
  FixedScroll(:addScroll="true")
    div(slot="fixed")
      PageHeader(pageTitle="入厂协同")

      ScanSearch(
          placeholder="预约单号"
          :initSearchValue="searchValue"
          @search="search"
          @scan="search"
          )

    g-v.info(v-if="info" slot="scroll")
      KvPair(k="预约单号" :v="info.ReserveNo")
      KvPair(k="实体仓库" :v="info.EWmNo")
      KvPair(k="需卸货实体仓库" :v="info.AllEWmNo")
      KvPair(k="预约时间" :v="info.ReservetimeDate | Date")
      KvPair(k="出厂时间" :v="info.LeaveTime | Date")
      KvPair(k="供应商" :v="info.SupplierNum")
      KvPair(k="姓名" :v="info.DriverName")
      KvPair(k="电话" :v="info.DriverPhone")
      KvPair(k="车牌号" :v="info.NumberPlateNo")
      KvPair(k="已卸货实体仓库" :v="info.DoneEWmNo")

    g-h.bottom-bar(slot="fixed-bottom" j-c="space-around" :wrap="true" v-if="info")
      Button(
          v-if="$root.User.permMap['LineUp(入厂管理_移动端)']"
          text="排队"
          @click="lineup"
          )
      Button(
          v-if="$root.User.permMap['Done(入厂管理_移动端)']"
          text="卸货完成"
          @click="done"
          )
      Button(
          v-if="$root.User.permMap['ToEnable(入厂管理_移动端)']"
          text="启用紧急Dock"
          @click="dock"
          )
      Button(
          v-if="$root.User.permMap['Leave(入厂管理_移动端)']"
          text="出厂"
          @click="leave"
          )

</template>

<script>
import api from "$api"
import util from "$util"

export default {
  data() {
    return {
      searchValue: "",
      isLoading: false,
      saveEnabled: false,
      loading: {},

      info: null,
    }
  },
  methods: {
    async search(value) {
      if (this.loading.search) {
        return
      }
      if (value === "") {
        return
      }

      this.loading.search = true
      try {
        let data = await api.CoordinationSearch({
          ReserveNo: value,
        })
        this.info = data.DockReserveInfo


        if (this.info) {
          let list = [
            "ReserveNo",
            "EWmNo",
            "AllEWmNo",
            "ReservetimeDate",
            "LeaveTime",
            "SupplierNum",
            "DriverName",
            "DriverPhone",
            "NumberPlateNo",
            "DoneEWmNo",
          ]
          for (let item of list) {
            this.info[item] = this.info[item] || ""
          }
        }

      } catch(err) {
        this.$root.error(err)
      } finally {
        this.isLoading = false
        this.loading.search = false
      }
    },

    // 排队
    async lineup() {
      if (this.loading.lineup) {
        return
      }
      if (!this.info) {
        return
      }

      this.loading.lineup = true
      try {
        await api.CoordinationLineUp({
          ReserveNo: this.info.ReserveNo,
        })
        util.Toast("排队成功")
      } catch(err) {
        this.$root.error(err)
      } finally {
        this.isLoading = false
        this.loading.lineup = false
      }
    },

    // 卸货完成
    async done() {
      if (this.loading.done) {
        return
      }
      if (!this.info) {
        return
      }

      this.loading.done = true
      try {
        await api.CoordinationDone({
          ReserveNo: this.info.ReserveNo,
        })
        util.Toast("卸货成功")
      } catch(err) {
        this.$root.error(err)
      } finally {
        this.isLoading = false
        this.loading.done = false
      }
    },

    // 启用紧急Dock
    async dock() {
      if (this.loading.dock) {
        return
      }
      if (!this.info) {
        return
      }

      this.loading.dock = true
      try {
        await api.CoordinationDock({
          ReserveNo: this.info.ReserveNo,
        })
        util.Toast("启用紧急Dock成功")
      } catch(err) {
        this.$root.error(err)
      } finally {
        this.isLoading = false
        this.loading.dock = false
      }
    },

    // 出厂
    async leave() {
      if (this.loading.leave) {
        return
      }
      if (!this.info) {
        return
      }

      this.loading.leave = true
      try {
        await api.CoordinationLeave({
          ReserveNo: this.info.ReserveNo,
        })
        util.Toast("出厂成功")
      } catch(err) {
        this.$root.error(err)
      } finally {
        this.isLoading = false
        this.loading.leave = false
      }
    },

    registerScanner() {
      util.registerScanner((data) => {
        this.searchValue = data
        this.search(data)
      })
    },
  },

  created() {
    this.registerScanner()
  },
}
</script>

<style lang="stylus" scoped>

.table
  scan-table()

.bottom-bar
  bottom: 10rem
  bottom-bar()

.bottom-bar > *
  margin-top: 5px

</style>
